﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Wicresoft.EKA.MVCExtensions;
using Wicresoft.EKA.ITSM.Business;
using Ninject;
using Wicresoft.EKA.ITSM.BusinessImpl;

namespace Wicresoft.EKA.ITSM.Web.WebPortal.Controllers
{
    public class InspectionTaskFrameAPIController : BaseAPIController
    {
        [Inject]
        public ISMGXJPlanTaskFrame SMGXJPlanTaskFrame { get; set; }

        [Inject]
        public ISMGXJPlanTask SMGXJPlanTask { get; set; }

        [Inject]
        public override IUserInfo UserInfo { get; set; }  //每个Controller必须包含的。

        protected override void Init()
        {
            ((IBase)SMGXJPlanTask).UserIdentify = UserName;
            ((IBase)SMGXJPlanTaskFrame).UserIdentify = UserName;
            base.Init();
        }

        protected override void Dispose(bool disposing)
        {
            ((IBase)SMGXJPlanTask).Dispose();
            ((IBase)SMGXJPlanTaskFrame).Dispose();
            base.Dispose(disposing);
        }

        //获取TaskFrame
        public object GetbyFrameID(string TaskID)
        {
            Init();
            string Qual = "'TaskID'=\"" + TaskID + "\"";
            return SMGXJPlanTaskFrame.GetFrame(Qual);
        }

        //更新TaskFame的值
        public object Put(string id, [FromBody] SMGXJPlanTaskFrameModel model)
        {
            try
            {
                Init();
                string result = string.Empty;
                model.ShortDescription = "Modify";
                model.cChr_Status = "normal";
                var list = SMGXJPlanTask.GetList("'GTicketID' = \"" + model.TaskID + "\"");
                foreach (var i in list)
                {
                    if (i.RcChr_RuleValue != "" && i.RcChr_RuleValue != null && i.RcChr_RuleValue != " ")
                    {
                        var data = i.RcChr_RuleValue.Split(';');
                        if (data.Length > 1 && i.RcChr_Char01 != data[0] && i.RcChr_Char01 != "" && i.RcChr_Char01 != null)
                        {
                            model.cChr_Status = "abnormal";
                            break;
                        }
                    }
                }
                string returnStr = SMGXJPlanTaskFrame.Modify("'Request ID' = \"" + id + "\"", model);
                return returnStr;
            }
            catch (Exception e)
            {
                return "Fail: " + e.Message;
            }
            
        }
    }
}
